package com.study.yamba;

import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import winterwell.jtwitter.android.AndroidTwitterLogin;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

public class StatusActivity extends Activity {
	
	public String msgContent="";
    Twitter twitter;
	private static final String TAG="statusActivity";
	private TextView tvCounter;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_status);
		
		
		Button buUpdate = (Button)findViewById(R.id.btnUpdate);
		EditText et = (EditText)findViewById(R.id.editText1);
		
//		twitter = new Twitter("student", "password");
	//	twitter.setAPIRootUrl("http://yamba.marakana.com/api");
		tvCounter = (TextView)findViewById(R.id.tvCount);
		tvCounter.setText("10");
		
		et.addTextChangedListener(onWatcher);
		
		msgContent= et.getText().toString();
		buUpdate.setOnClickListener(onUpdate);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
		case R.id.pref_setting:
			startActivity(new Intent(this, PrefActivity.class));
			break;
		}
		return true;
	}
	
	
	public TextWatcher onWatcher = new TextWatcher()
	{

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			int count = 10- arg0.length();
			tvCounter.setText(Integer.toString(count));
			if(count<5)
			{
			    tvCounter.setTextColor(Color.YELLOW);
			}
			
			if(count<2)
			{
				tvCounter.setTextColor(Color.RED);
			
			}
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	public View.OnClickListener onUpdate = new View.OnClickListener() {
		
		@SuppressWarnings("deprecation")
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.d(TAG, msgContent);
			
			TextView tv = (TextView)findViewById(R.id.tvInfo);
		
           TaskManager tm =  new TaskManager();
           tm.execute(tv.getText().toString());
			
			tv.setText(msgContent); //.Text=msgContent;
			Toast.makeText(StatusActivity.this, "update done!", Toast.LENGTH_LONG).show();
		}
	};
	
	
	class TaskManager extends AsyncTask<String, Integer, String>
	{

		@Override
		protected String doInBackground(String...status)
		{
			try
			{
			
				OAuthSignpostClient oauthClient = new OAuthSignpostClient("nxKhs1UFKANSXBcWtX7Q"
						, "v8atyb4XdO6CBbyTNNASnSUarUvh9za5zkyovhcNAU", null);

				// open the authorization page in the user's browser
				/*URI url = client.authorizeUrl();
				Intent myIntent = new Intent(Intent.VIEW_ACTION);
				myIntent.setData(url);
				startActivity(myIntent);

				// TODO Get the pin from the user 
				String pinCode;

				oauthClient.setAuthorizationCode(pinCode);
				// Store the authorization token details for future use
				Object accessToken = client.getAccessToken();*/

				// Make a Twitter object
				Twitter twitter = new Twitter("zackyChina", oauthClient);
				// Set your status
				twitter.setStatus("Messing about in Java");
			//twitter = new Twitter("zackyChina", "zacky8010");
			//twitter.setStatus(tv.getText().toString());
				return "done";
			}
			catch(TwitterException ex)
			{
				Toast.makeText(StatusActivity.this, "login twitter failed"+ex.getMessage().toString(), Toast.LENGTH_LONG).show();
				return "failed twitter";
			}
		
		}
		
		
		@Override
		protected void onPostExecute(String result)
		{
			Toast.makeText(StatusActivity.this, "execute" , Toast.LENGTH_LONG).show();
		}
	
		
		@Override
		protected void onProgressUpdate(Integer...progress)
		{
			super.onProgressUpdate(progress);
		}
	};

}


